package com.maoye.ossp.biz.contract.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.brand.service.IBrandCateService;
import com.maoye.ossp.biz.brand.service.IBrandLevelService;
import com.maoye.ossp.biz.brand.service.IBrandReviewService;
import com.maoye.ossp.biz.brand.service.ISupplierRegisterService;
import com.maoye.ossp.biz.contract.entity.Assbaseitems;
import com.maoye.ossp.biz.contract.entity.Assbrands;
import com.maoye.ossp.biz.contract.entity.Assitems;
import com.maoye.ossp.biz.contract.entity.AssociatedContract;
import com.maoye.ossp.biz.contract.entity.Assshops;
import com.maoye.ossp.biz.contract.entity.ResultSapVo;
import com.maoye.ossp.biz.contract.service.IAssbaseitemsService;
import com.maoye.ossp.biz.contract.service.IAssbrandsService;
import com.maoye.ossp.biz.contract.service.IAssbshService;
import com.maoye.ossp.biz.contract.service.IAssitemsService;
import com.maoye.ossp.biz.contract.service.IContractService;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.contract.utils.JacksonUtils;
import com.maoye.ossp.bpm.workflow.service.IProcessService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.service.IDataBean;
import com.maoye.ossp.common.util.ApplicationContextUtils;
import com.maoye.ossp.common.util.RequestContext;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.BerthMaindata;
import com.maoye.ossp.sys.base.entity.CMCate;
import com.maoye.ossp.sys.base.entity.SysBaseShop;
import com.maoye.ossp.sys.base.service.BerthMaindataService;
import com.maoye.ossp.sys.base.service.ICMCateService;
import com.maoye.ossp.sys.base.service.SysBaseFloorService;
import com.maoye.ossp.sys.base.service.SysBaseFormatService;
import com.maoye.ossp.sys.base.service.SysBaseShopFormatService;
import com.maoye.ossp.sys.base.service.SysBaseShopRegionService;
import com.maoye.ossp.sys.base.service.SysBaseShopService;
import com.maoye.ossp.sys.base.service.SysBaseTaxRateService;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.api.local.UserService;
import com.maoye.sc.service.user.UserServiceImpl;

@Controller
@RequestMapping(value = "biz/contract/associated/")
public class ContractController extends BaseController{
	@Resource
	private IContractService  contractServce;
	@Resource
    private IAssbrandsService assbrandService;  //合同品牌信息
	@Resource
	private IAssbshService assbshService;     //品牌铺位关系
	@Resource
	private IAssitemsService   assitemsService;  //合同条款
	@Resource
	private IAssbaseitemsService assbaseitemsService; //销售分成及结算条款
	@Resource
	private ISupplierRegisterService supplierRegisterService;
	@Resource
	private SysBaseTaxRateService sysBaseTaxRateService;
	@Resource
	private SysBaseFormatService sysBaseFormatService ;
	@Resource
	private IBrandCateService brandCateService;		//品牌信息
	@Resource
	private ICMCateService icmCateService;		//招商分类
	@Resource
	private SysBaseShopService sysBaseShopService;
	@Resource 
	private IBrandReviewService brandReviewService;
	@Resource
	private SysBaseShopFormatService sysBaseShopFormatService;
	@Resource
	private IBrandLevelService  brandLevelService; //品牌等级
	@Resource
	private BerthMaindataService brtThservice;
	@Resource
	private SysBaseShopRegionService shopRegionService;
	@Resource
	private SysBaseFloorService sysBaseFloorService;
	
	@Resource
	private UserServiceImpl  userServiceImpl;
	
	@Resource
	IProcessService processService;

	@RequestMapping(value = "list")
	public String List(AssociatedContract associatedContract,HttpServletRequest request,HttpServletResponse response, Model model){
		String createById =associatedContract.getCreateBy();
		String createBy = null;
		if(StringUtil.isNotEmpty(createById)){
			createBy = createById.split("\\|")[0];
		}
		QueryFilter queryFilter = getQuerFilter(request,associatedContract);
		queryFilter.addFilter("areaId", associatedContract.getAreaId(), QueryOP.EQUAL);
		queryFilter.addFilter("floorId", associatedContract.getFloorId(), QueryOP.EQUAL);
		queryFilter.addFilter("storeId", associatedContract.getStoreId(), QueryOP.EQUAL);
		queryFilter.addFilter("startDate", associatedContract.getStartDate(), QueryOP.EQUAL);
		queryFilter.addFilter("endDate", associatedContract.getEndDate(), QueryOP.EQUAL);
		queryFilter.addFilter("historyContractNo", associatedContract.getHistoryContractNo(), QueryOP.EQUAL);
		
		if(StringUtil.isNotEmpty(associatedContract.getContractNo())){
			queryFilter.addFilter("contractNo", associatedContract.getContractNo(), QueryOP.EQUAL);
		}
		if(StringUtil.isNotEmpty(associatedContract.getSignBy())){
			queryFilter.addFilter("signBy", associatedContract.getSignBy(), QueryOP.LIKE);
		}
		
		if(StringUtil.isNotEmpty(createBy)){
			queryFilter.addFilter("createBy", createBy, QueryOP.LIKE);
		}
		queryFilter.addFilter("contractStatus", associatedContract.getContractStatus(), QueryOP.EQUAL);
		queryFilter.addFilter("contractAttribute", associatedContract.getContractAttribute(), QueryOP.EQUAL);
	
		queryFilter.addFilter("isNew", 0, QueryOP.EQUAL);
		Page<AssociatedContract> page = null;
		if(associatedContract.getAssbrandsList() !=null && !associatedContract.getAssbrandsList().isEmpty()){
			Assbrands brand = associatedContract.getAssbrandsList().get(0);
			if(StringUtil.isNotEmpty(brand.getBrandCName())){
				queryFilter.addFilter("brandCName", brand.getBrandCName(), QueryOP.EQUAL);
				page = (Page<AssociatedContract>) contractServce.find1(queryFilter);
				model.addAttribute("brandCName", brand.getBrandCName());
			}else{
				page = (Page<AssociatedContract>) contractServce.find(queryFilter);
			}
			
		}else{
			page = (Page<AssociatedContract>) contractServce.find(queryFilter);
		}
		
		model.addAttribute("page", page);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/contract/associated/list", model, request);
	}
	
	@RequestMapping(value = "error")
	public String error(AssociatedContract associatedContract,HttpServletRequest request,HttpServletResponse response, Model model,String errormessage){
		
		model.addAttribute("errormessage", errormessage);
		return resultPage("/biz/contract/associated/error", model, request);
		
	}
	@RequestMapping(value = "getSelectJson")
	public void getSelectJson(HttpServletRequest request,HttpServletResponse response, Model model) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.write("[{\"provinceID\":\"110000\",\"province\":\"百丽\"},{\"provinceID\":\"120000\",\"province\":\"百丽2 \"}]");
		out.flush();
		out.close();
		
	}
	
	@RequestMapping(value = "{mainId}/{versionId}/{assistId}/getHistoryVersion")
	public String getHistoryVersion(@PathVariable String mainId,@PathVariable String versionId,@PathVariable String assistId,
			HttpServletRequest request,HttpServletResponse response, Model model) {
		HashMap<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("mainId", mainId); 
		queryMap.put("versionId", versionId); 
		queryMap.put("assistId", assistId);  
		List<AssociatedContract> list = contractServce.getHistoryVersion(queryMap); 
		model.addAttribute("list", list);
		return resultPage("/biz/contract/common/versionList",model,request);
	}
	
	 /* 新建版本，读取历史版本数据用于快速的显示  createNewVersion
	  * @param id  主键ID
	  * @param request
	  * @param response
	  * @param assistId 最新版本号
	  * @param model
	  * @return
	  * @throws Exception
	  */
	@RequestMapping(value = "{id}/{historyId}/createNewVersion")
	public String createNewVersion(@PathVariable String id,@PathVariable String historyId,HttpServletRequest request,HttpServletResponse response,
			String assistId, Model model) throws Exception{
		List<Assbrands>  _assbrandList = new ArrayList<Assbrands>();
		List<Assbaseitems> _assbaseitemsList=new ArrayList<Assbaseitems>();
		List<Assitems> _assitemsList = new ArrayList<Assitems>();
		StringBuffer shopName = new StringBuffer();
		StringBuffer shopId = new StringBuffer();
		String contractOperate = request.getParameter("contractAttribute");          //合同变更，续签，新签，先生号
		//合同基本信息
		AssociatedContract associated = contractServce.createNewVersion(id,assistId,historyId);  
		associated.setId(null);
		List<Assshops> shopsList=associated.getAssshopsList();
		
		for (int i = 0; i < shopsList.size(); i++) {
			shopName.append(shopsList.get(i).getShop());
			shopName.append(",");
			shopId.append(shopsList.get(i).getShopId());
			shopId.append(",");
		}
		
		//合同品牌信息
		List<Assbrands>  assbrandList= assbrandService.getAssbrandsByContractId(id);
		for (Assbrands brand :assbrandList) {
			brand.setId(null);
			_assbrandList.add(brand);
		}
		//销售分成及结算条款
		List<Assbaseitems> assbaseitemsList= assbaseitemsService.getAssbaseitemsByContractId(id);
		
		for (Assbaseitems assbaseitems : assbaseitemsList) {
			assbaseitems.setId(null);
			_assbaseitemsList.add(assbaseitems);
		}
		//合同条款
		List<Assitems> assitemsList=assitemsService.getAssitemsByContractId(id);

		for (Assitems assitems : assitemsList) {
			assitems.setId(null);
			_assitemsList.add(assitems);
		}
		//非变更合同
		if(!contractOperate.equals(ContractUtils.CONTRATC_ATTRIBUTE_BG)){
			associated.setContractAttribute(contractOperate);
			associated.setIsModifyContract(null);
		}else {
			associated.setIsModifyContract(ContractUtils.CONTRCT_MODIFY_Y);

		}
		associated.setContractStatus(ContractUtils.CONTRACT_STATE_SAVE);
		associated.setLastContractId(Long.parseLong(id));
		model.addAttribute("shopName", shopName);
		model.addAttribute("shopId", shopId);
		model.addAttribute("assbrand",_assbrandList);
		model.addAttribute("assbaseitems",_assbaseitemsList);
		model.addAttribute("assitems",_assitemsList);
		model.addAttribute("associatedContract", associated);
		return  resultPage("/"+associated.getVersionUrl()+"/add",model,request);
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="{id}/{terminateDate}/close")
	public String closeContract(@PathVariable Long id ,@PathVariable Date terminateDate,HttpServletRequest request,AssociatedContract associatedContract){
		String navId = request.getParameter("navId");
		HashMap map = new HashMap();
		map.put("id", id);
		map.put("terminateDate", terminateDate);
		map.put("contractStatus", ContractUtils.CONTRACT_STATE_CLOSE);
		map.put("contractAttribute", ContractUtils.CONTRATC_ATTRIBUTE_ZZ);
		contractServce.updateContractSate(map);
		return "redirect:/biz/contract/associated/list?navId="+navId;
	}

	@RequestMapping(value="add")
	public String add(AssociatedContract associatedContract,HttpServletRequest request,HttpServletResponse response, Model mode){
		//associatedContract.setRecieveStatus("0");
		associatedContract.setIsUndertakeElec("0");
		associatedContract.setContractAttribute(ContractUtils.CONTRATC_ATTRIBUTE_XINQ);
		associatedContract.setRecieveStatus(ContractUtils.CONTRACT_TEXT_STATE_WLQ);
		associatedContract.setContractStatus(ContractUtils.CONTRACT_STATE_SAVE);
		associatedContract.setIsJobCard("0");
		associatedContract.setIsBrandImage("0");
		associatedContract.setIsTraining("0");
		associatedContract.setWorkClothes("0");
		associatedContract.setSignById(UserUtils.getCurrentUserId().toString());
		associatedContract.setVersionUrl("biz/contract/associated/");
		associatedContract.setSignBy(UserUtils.getUser().getName());
		associatedContract.setCreateBy(UserUtils.getCurrentUserId().toString());
		mode.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/contract/associated/add", mode, request) ;
	}
	
	/**
	 * 合同续签
	 * @param associatedContract
	 * @param request
	 * @param response
	 * @param mode
	 * @return
	 * @author yangh
	 * @data 2014年4月26日
	 */
	@RequestMapping(value="{id}/goon")
	public String goon(@PathVariable Long id ,HttpServletRequest request,HttpServletResponse response, Model model){
		List<Assbrands>  _assbrandList = new ArrayList<Assbrands>();
		List<Assbaseitems> _assbaseitemsList=new ArrayList<Assbaseitems>();
		List<Assitems> _assitemsList = new ArrayList<Assitems>();
		StringBuffer shopName = new StringBuffer();
		StringBuffer shopId = new StringBuffer();
		
		//合同基本信息
		if(id ==null){
			id=0L;
		}
		AssociatedContract associated = contractServce.get(id.toString());  
		associated.setId(null);
		List<Assshops> shopsList=associated.getAssshopsList();
		
		for (int i = 0; i < shopsList.size(); i++) {
			shopName.append(shopsList.get(i).getShop());
			shopName.append(",");
			shopId.append(shopsList.get(i).getShopId());
			shopId.append(",");
		}
		
		//合同品牌信息
		List<Assbrands>  assbrandList= assbrandService.getAssbrandsByContractId(id.toString());
		for (Assbrands brand :assbrandList) {
			brand.setId(null);
			_assbrandList.add(brand);
		}
		//销售分成及结算条款
		List<Assbaseitems> assbaseitemsList= assbaseitemsService.getAssbaseitemsByContractId(id.toString());
		
		for (Assbaseitems assbaseitems : assbaseitemsList) {
			assbaseitems.setId(null);
			_assbaseitemsList.add(assbaseitems);
		}
		//合同条款
		List<Assitems> assitemsList=assitemsService.getAssitemsByContractId(id.toString());

		for (Assitems assitems : assitemsList) {
			assitems.setId(null);
			_assitemsList.add(assitems);
		}
		associated.setContractAttribute(ContractUtils.CONTRATC_ATTRIBUTE_XUQ);
		associated.setRecieveStatus(ContractUtils.CONTRACT_TEXT_STATE_WLQ);
		associated.setContractStatus(ContractUtils.CONTRACT_STATE_SAVE);
		associated.setLastContractId(id);
		associated.setCreateTime(new Date());
		associated.setCreateBy(UserUtils.getCurrentUserId().toString());
		//非变更合同
		associated.setIsModifyContract(null);
		model.addAttribute("shopName", shopName);
		model.addAttribute("shopId", shopId);
		model.addAttribute("assbrand",_assbrandList);
		model.addAttribute("assbaseitems",null);
		model.addAttribute("assitems",null);
		model.addAttribute("associatedContract", associated);
		return resultPage("/biz/contract/associated/add", model, request) ;
	}
	

	@RequestMapping(value = "save")
	public String save(AssociatedContract associatedContract, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response,String remove_Contact,String delContactId) throws Exception {
		String navId = request.getParameter("navId");
		if(StringUtil.isEmpty(associatedContract.getOperateState())){
			associatedContract.setOperateState(ContractUtils.CONTRACT_STATE_SUBMIT_SAP_N);
		}
		contractServce.saveContract(associatedContract,remove_Contact,delContactId);
		addMessage(redirectAttributes, "保存成功:"+associatedContract.getContractNo());
		return "redirect:/biz/contract/associated/list?navId="+navId;
	}
	
	@RequestMapping(value = "submit")
	public String submit(AssociatedContract associatedContract, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response,String remove_Contact,String delContactId) throws Exception {
		String navId = request.getParameter("navId");
		if(associatedContract.getContractStatus().equals(ContractUtils.CONTRACT_STATE_SAVE)){
			associatedContract.setContractStatus(ContractUtils.CONTRACT_STATE_UNAUDIT);
		}
		
		if(StringUtil.isEmpty(associatedContract.getOperateState())){
			associatedContract.setOperateState(ContractUtils.CONTRACT_STATE_SUBMIT_SAP_N);
		}
		
		contractServce.saveContract(associatedContract,remove_Contact,delContactId);
		
		if(associatedContract.getContractStatus()==ContractUtils.CONTRACT_STATE_SAVE||associatedContract.getContractStatus()==ContractUtils.CONTRACT_STATE_UNAUDIT){
			Map<String, Object> params = new HashMap<String, Object>();
			HashMap<String, String> map = Maps.newHashMap();
			map.put("no", associatedContract.getStoreId());
			SysBaseShop shop =sysBaseShopService.getByNo(map);
			String contractId = shop.getContractManager();
			params.put("contractId",contractId); 
			processService.executeWorkFlow(associatedContract.getId().toString(), params);
		}
		addMessage(redirectAttributes, "保存成功:"+associatedContract.getContractNo());
		return "redirect:/biz/contract/associated/list?navId="+navId;
	}
	@RequestMapping(value ="{id}/view")
	public String view(@PathVariable String id,Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		//合同基本信息
		AssociatedContract associated = contractServce.getAssociatedById(id);
		
		StringBuffer shopName = new StringBuffer();
		StringBuffer shopId = new StringBuffer();
		List<Assshops> shopsList=associated.getAssshopsList();
		
		for (int i = 0; i < shopsList.size(); i++) {
			shopName.append(shopsList.get(i).getShop());
			shopId.append(shopsList.get(i).getShopId());
			if(i!=shopsList.size()-1){
				shopName.append(",");
				shopId.append(",");
			}
		}

		if(StringUtil.isNotEmpty(associated.getCreateBy())){
			
			User user =userServiceImpl.getUserById(associated.getCreateBy());
			if(user!=null){
				model.addAttribute("createBy",user.getName());
			}
		}
		model.addAttribute("shopName",shopName);
		model.addAttribute("shopId", shopId);
		model.addAttribute("associated",associated);
		model.addAttribute("id",id);
		model.addAttribute("assbrand",associated.getAssbrandsList());
		model.addAttribute("assbaseitems",associated.getAssbaseitemsList());
		model.addAttribute("assitems",associated.getAssitemsList());
		model.addAttribute("itemCodeList", associated.getAssTempItemCodeList());
		return resultPage("/biz/contract/associated/view",model,request);
	}
	
	
	@RequestMapping(value = "{ids}/removeBatch")
	public String removeBatch(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) {
		String navId = request.getParameter("navId");
		String nos = request.getParameter("nos");
		 String ip = request.getRemoteAddr();
		 try {
			 contractServce.removeBatch(ids,nos,ip);
			 addMessage(redirectAttributes, nos+"合同已删除！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			 addMessage(redirectAttributes, "合同删除失败！");
		}
		
		redirectAttributes.addFlashAttribute(navId);
		return "redirect:/biz/contract/associated/list?navId="+navId;
	}
	
	/**
	 * @throws Exception 
	 * 批量提交 SAP
	* @Title: removeBatch
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param ids
	* @param @param request
	* @param @param model
	* @param @param redirectAttributes
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "{ids}/submitSap")
	public String submitSap(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) throws Exception {
		String navId = request.getParameter("navId");
		String pageNo =request.getParameter("pageNo");
		IDataBean dataBean = (IDataBean) ApplicationContextUtils.getBean("commonLYSendSapService");
		String[] idarr =ids.split(",");
		for (int i = 0; i < idarr.length; i++) {
			AssociatedContract contract=contractServce.get(idarr[i]);
			if(contract.getContractStatus().equals(ContractUtils.CONTRACT_STATE_SAVE)||
					contract.getContractStatus().equals(ContractUtils.CONTRACT_STATE_UNAUDIT)
					||ContractUtils.CONTRACT_STATE_SUBMIT_SAP_S.equals(contract.getOperateState())){
				continue;
			}
			request.setAttribute("id", idarr[i]);
			RequestContext requestContext = new RequestContext(request);
			List<Object> dataList = dataBean.getDataList(requestContext);
			for (Object object : dataList) {
				ResultSapVo vo =(ResultSapVo)object;
				HashMap<String, Object> map = Maps.newHashMap();
				map.put("id", idarr[i]);
				map.put("operateState", vo.getStatus());
				contractServce.updSapStatus(map);
			}
		}

		return "redirect:/biz/contract/associated/list?navId="+navId+"&pageNo="+pageNo;
	}
	
	
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,HttpServletRequest request,RedirectAttributes redirectAttributes) {
		String navId = request.getParameter("navId");
		String nos = request.getParameter("nos");
		String names = request.getParameter("names");
		contractServce.removeBatch(id,nos,names);
		redirectAttributes.addFlashAttribute(navId);
		return "redirect:/biz/contract/associated/list?navId="+navId;
	}
	
	/***
	 * 确认
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author lingzhizhong
	 * @data 2014年3月11日
	 */
	@RequestMapping(value = "{id}/confirmContract")
	public String confirmContract(@PathVariable String id,Model model,RedirectAttributes redirectAttributes){
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("contractStatus", "2");
		contractServce.confirmContract(map);
		
		return "redirect:/biz/contract/associated/list";
		
	}
		
	/***
	 * 存档
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author lingzhizhong
	 * @data 2014年3月11日
	 */
	@RequestMapping(value = "{id}/keepContract")
	public String keepContract(@PathVariable String id,Model model,RedirectAttributes redirectAttributes){
		return "redirect:/biz/contract/associated/list";
		
	}
	
	/***
	 * 领取
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author lingzhizhong
	 * @data 2014年3月11日
	 */
	@RequestMapping(value = "{id}/receiveContract")
	public String receiveContract(@PathVariable String id,Model model,RedirectAttributes redirectAttributes,HttpServletRequest request){
		String navId = request.getParameter("navId");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("recieveStatus", ContractUtils.CONTRACT_TEXT_STATE_YLQ);
		contractServce.receiveContract(map);
		addMessage(redirectAttributes, "领取成功:");
		return "redirect:/biz/contract/associated/list?navId="+navId;
		
	}
	
	@RequestMapping(value = "getVerionHref")
	public String getVerionHref(Model model,RedirectAttributes redirectAttributes){
		
		
		
		return "/biz/contract/associated/version";
		
	}
	/**********
	 * 合同修
	 * 
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author eric
	 * @data 2014年3月7日
	 */
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model,RedirectAttributes redirectAttributes,HttpServletRequest request) {

		StringBuffer shopName = new StringBuffer();
		StringBuffer shopId = new StringBuffer();
		String contractOperate = request.getParameter("contractAttribute");          //合同变更，续签，新签，先生号
		//合同基本信息
		AssociatedContract associated = contractServce.getAssociatedById(id);  
		List<Assshops> shopsList=associated.getAssshopsList();
		for (int i = 0; i < shopsList.size(); i++) {
			shopName.append(shopsList.get(i).getShop());
			shopName.append(",");
			shopId.append(shopsList.get(i).getShopId());
			shopId.append(",");
		}
		associated.setLastContractId(Long.parseLong(id));
		model.addAttribute("shopName", shopName);
		model.addAttribute("shopId", shopId);
		model.addAttribute("assbrand",associated.getAssbrandsList());
		model.addAttribute("assbaseitems",associated.getAssbaseitemsList());
		model.addAttribute("assitems",associated.getAssitemsList());
		model.addAttribute("associatedContract", associated);
		return  resultPage("/"+associated.getVersionUrl()+"/add",model,request);
	
	}
	
	
	/**
	 * 先申请号合同转正式合同
	* @Title: FormalContract
	* @param @param model
	* @param @param redirectAttributes
	* @param @param request
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="formalContract")
	public String formalContract(Model model,RedirectAttributes redirectAttributes,HttpServletRequest request){
		String navId = request.getParameter("navId");
		String id = request.getParameter("id");

		AssociatedContract associated = contractServce.getAssociatedById(id);
		associated.setContractAttribute(ContractUtils.CONTRATC_ATTRIBUTE_XINQ);
		associated.setIsModifyContract(ContractUtils.CONTRCT_MODIFY_Y);
		contractServce.update(associated);
		addMessage(redirectAttributes, "转正式合同成功:"+associated.getContractNo());
		return "redirect:/biz/contract/associated/list?navId="+navId;
		
	}
	
	
	/**
	 * 获取最新的一条数据  getMainVersion
	 * 		创建“新版本”时，获取最新的版本信息返回verisonView.jsp页面
	 * @param student
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "{mainId}/{historyId}/{id}/getMainVersion")
	public String getMainVersion(@PathVariable String mainId,@PathVariable String historyId,@PathVariable Long id, HttpServletRequest request,Model model) {
		HashMap queryvalidate = new HashMap();
		String contractAttribute = request.getParameter("contractAttribute");
		queryvalidate.put("mainId", mainId);
		AssociatedContract contract=contractServce.getMainVersion(queryvalidate); 
		contract.setHistoryId(historyId);
		//创建“新版本”时，获取最新的版本信息返回verisonView.jsp页面
		model.addAttribute("version", contract);
		model.addAttribute("contractAttribute", contractAttribute);
		return resultPage("/sys/version/versionModal",model,request);
	} 
	@RequestMapping(value = "getModuleContractList")
	public String getModuleContractList(HttpServletRequest request,HttpServletResponse response, Model mode){
		 
		ArrayList<AssociatedContract> list =(ArrayList<AssociatedContract>) contractServce.getAssociatedContractList(null);
		mode.addAttribute("contractList", list);
		return "/biz/intent/intAssociated/modal";
	}
	/**
	 *选择供应商信息
	 * @param model
	 * @return
	 * @author llz
	 * @data 2014年4月24日
	 */
	@RequestMapping(value = "getSupplierList")
	public String getSupplierList(Model model) {
		
		return "/biz/contract/associated/supplierList";
	}
	
	
	@RequestMapping(value = "editAssbrand")
	public String editAssbrand(Model model,RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response,String assBrand,String operate,String num) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String _brand=assBrand.replace("'", "\"");
		SimpleFilterProvider fp = new SimpleFilterProvider();
		fp.addFilter(
				"executeFilter",
				SimpleBeanPropertyFilter.serializeAllExcept(new String[] {
				 "createBy" }));
		Assbrands brand =JacksonUtils.create().filter("myFilter", "id").json2Obj(_brand, Assbrands[].class)[0];
		model.addAttribute("brand", brand);
		CMCate cmcCate = null;
		CMCate cmcCate2 = null;
		if(brand.getInviteCate()!=null){
			cmcCate = icmCateService.getByCateNo(brand.getInviteCate());
			
		}
		if(brand.getPurchasingGroup()!=null){
			cmcCate2= icmCateService.getByCateNo(brand.getPurchasingGroup());
		}
		model.addAttribute("cmcCate", cmcCate);
		model.addAttribute("purchasingGroup", cmcCate2);
		model.addAttribute("operate","edit");
		model.addAttribute("num",num);
		return "/biz/contract/common/modalBarnd";
	}
	
	
	@RequestMapping(value = "addAssitems")
	public String addAssitems(Model model) {
		model.addAttribute("operate","add");
		model.addAttribute("contractType","associated");
		return "/biz/contract/common/modalitems";
	}
	
	@RequestMapping(value = "editAssitems")
	public String editAssitems(Model model,String itemsJson,String operate,String num ,HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		String _items=itemsJson.replace("'", "\"");
		SimpleFilterProvider fp = new SimpleFilterProvider();
		fp.addFilter(
				"executeFilter",
				SimpleBeanPropertyFilter.serializeAllExcept(new String[] {
				 "createBy" }));
		Assitems items =JacksonUtils.create().filter("myFilter", "id").json2Obj(_items, Assitems[].class)[0];
		model.addAttribute("items", items);
		model.addAttribute("operate","edit");
		model.addAttribute("contractType", "associated");
		model.addAttribute("num",num);
		return "/biz/contract/common/modalitems";
	}
	 
	
	@RequestMapping(value = "addModalBase")
	public String addModalBase(Model model) {
		model.addAttribute("operate","add");
		return "/biz/contract/associated/modalBase";
	}
	
	
	@RequestMapping(value = "editModalBase")
	public String editModalBase(Model model,String baseJson,String operate,String num ) {
		
		String _base=baseJson.replace("'", "\"").replace("@", "*").replace("#", "*");
		SimpleFilterProvider fp = new SimpleFilterProvider();
		fp.addFilter(
				"executeFilter",
				SimpleBeanPropertyFilter.serializeAllExcept(new String[] {
				 "createBy" }));
		Assbaseitems base =JacksonUtils.create().filter("myFilter", "id").json2Obj(_base, Assbaseitems[].class)[0];
		model.addAttribute("base", base);
		model.addAttribute("operate","edit");
		model.addAttribute("num",num);
		return "/biz/contract/associated/modalBase";
	}
	
	@RequestMapping(value ="{id}/importContract")
	public String importContract(@PathVariable  String id,HttpServletRequest request,HttpServletResponse response, Model model) throws IOException{
		
		return "/biz/contract/associated/view";
	}
	
	
	
	

	@RequestMapping(value="showModal")
	public String showModal(BerthMaindata berthMaindata, Model model){
		List<BerthMaindata> list = brtThservice.getByFloor(berthMaindata);
		model.addAttribute("list", list);
		return "/biz/contract/associated/modalBerth";
	}
}